from game import zz_game
from zz import zz_log, zz_img

log = zz_log.of()


class Game(zz_game.ZZ_Game):
    def __init__(self, d, has_prize=True):
        super().__init__(d, has_prize)

    def play_only_1(self):
        return True

    def check_done(self):
        return False

    def get_name(self):
        return "武侠大明星"

    def game_alert(self):
        self.alert_close_by_file("./game/img/武侠大明星")

    def play_alert(self):
        self.alert_close_by_file("./game/img/武侠大明星", 0.8, 'playxx_', True)

    def handle_main(self):
        # if self.img_exit("./game/img/武侠大明星/start.png", 20):
        #     self.log_msg("点击开始游戏")
        #     self.img_click("./game/img/武侠大明星/start.png")
        self.log_msg("等待进入游戏主界面")
        if not self.img_exit("./game/img/武侠大明星/game_main.png", 30):
            return False
        self.log_msg("已进入游戏主界面")

        for i in range(2):
            self.game_alert()
            # self.img_click_if_exists("点击开始挑战", "./game/img/武侠大明星/game_start.png", "./game/img/武侠大明星/game_start_01.png", key='first')
            if i <= 0:
                self.img_click_if_exists("点击开始挑战", "./game/img/武侠大明星/game_start.png", "./game/img/武侠大明星/game_start_01.png", key='first')
            else:
                # 第2次已经选中关卡1
                self.log_msg("点击开始挑战")
                self.img_click("./game/img/武侠大明星/game_start.png", key="first")
            self.img_click_if_exists("点击关卡1", "./game/img/武侠大明星/game_start_01.png", "./game/img/武侠大明星/game_start_02.png")
            self.img_click_if_exists("点击关卡1-1", "./game/img/武侠大明星/game_start_02.png", "./game/img/武侠大明星/game_start_03.png")
            self.log_msg("点击开始挑战")
            self.img_click("./game/img/武侠大明星/game_start_03.png")
            self.log_msg("等待进入挑战主界面")
            self.img_exit("./game/img/武侠大明星/play_main.png", 30)
            self.log_msg("已进入挑战主界面")
            self.log_msg("选择队长")
            w, h = self.d.window_size()
            self.d.click(w / 2, h / 2)

            while True:
                if (self.img_exit("./game/img/武侠大明星/play_alert.png", 1)
                        or self.img_exit("./game/img/武侠大明星/play_alert_01.png", 1)):
                    self.log_msg("存在选择界面，进行选择")
                    w, h = self.d.window_size()
                    self.d.click(w / 2 - 100, h / 2)
                    continue

                if self.img_exit("./game/img/武侠大明星/play_end.png", 1):
                    break

                self.play_alert()

                self.d.sleep(2)
            self.log_msg(f"战斗结束，点击确定")
            self.img_click('./game/img/武侠大明星/play_end_01.png')

        return True
